// pages/withdrawals/withdrawals.js
const app = getApp()
const url = app.basicsUrl();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount: false,
    balance: '',
    money: '',
    tokenKey: '',
    token: '',
    openId: '',
  },

  /**
   * 点击跳转常见问题
   */
  bindproblem: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/problem/problem'
    })
  },

  /**
   * 提现formSubmit
   */
  formSubmit: function (e) {
    var that = this;
    if (e.detail.value.money == '') {
      wx.showLoading({
        title: '金额不能为空！',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    } else if (e.detail.value.money > that.data.balance) {
      wx.showLoading({
        title: '超过账号余额！',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    } else if (e.detail.value.money < 1) {
      wx.showLoading({
        title: '不能少于1元！',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    } else {
      var params = {};
      params['money'] = (e.detail.value.money * 100).toFixed(0);
      params['tokenKey'] = that.data.tokenKey,
      params['token'] = that.data.token,
      params['openId'] = that.data.openId,
      wx.request({
        url: url + 'authorized/withdrawCashVo',
        data: params,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: res => {
          if (res.data.code == 200) {
            wx.showLoading({
              title: res.data.msg,
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/mine/mine'
              })
            }, 1000)
          } else {
            wx.showLoading({
              title: res.data.msg,
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }
        }
      })
    }
  },

  /**
   * 全部提现
   */
  bindcashall:function(){
    var that = this;
    that.setData({
      money: that.data.balance,
      amount: true,
    })
  },

  /**
   * 输入价格
   */
  bindinputamount: function (e) {
    var that = this;
    if(e.detail.value !== ''){
      that.setData({
        amount: true,
      })
    } else {
      that.setData({
        amount: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        that.setData({
          tokenKey: res.data.id,
          token: res.data.token,
          openId: res.data.openId,
        })
        var params = {};
        params['tokenKey'] = res.data.id,
        params['token'] = res.data.token,
        params['openId'] = res.data.openId,
        wx.request({
          url: url + "authorized/getRecentUserInfo",
          data: params,
          header: {
            'content-type': 'application/json'
          },
          success: res => {
            if (res.data.code == 200) {
              that.setData({
                balance: res.data.data.wallet/100,
              })
            } else {
              wx.showLoading({
                title: res.data.msg,
              })
              setTimeout(function () {
                wx.hideLoading()
              }, 1000)
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})